//
//  AltMyViewController.m
//  JHZWeiBo
//
//  Created by 张德培 on 16/5/28.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "AltMyViewController.h"
#import "MessageTitleModel.h"
@interface AltMyViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSArray *titleArray;

@end

@implementation AltMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *setButton = [[UIBarButtonItem alloc]initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(setButtonAction:)];
    self.navigationItem.rightBarButtonItem = setButton;
    
    
    
    
    
    NSMutableArray *modelArray = [self creatTitleArrayWithArray:_arrayTitle withUrlArray:_arrayURL];
    [self creatTitleButtonWithFrame:CGRectMake(0, 0, 100,20) withTitleArray:modelArray];
    
  
    
}

#pragma  marks --- 创建一个标题按钮 ---
-  (void)creatTitleButtonWithFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray{
    self.mainTitleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.mainTitleButton.frame = CGRectMake(0, 0, 100,20);
    self.mainTitleButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.mainTitleButton setTitle:[titleArray[1] title] forState:UIControlStateNormal];
    [self.mainTitleButton addTarget:self action:@selector(mainTitleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = self.mainTitleButton;
    
    
    
     _titleArray = titleArray;
    self.minTableView = [[UITableView alloc]initWithFrame:CGRectMake(110,64, kWidth - 220, 20 * _titleArray.count ) style:UITableViewStylePlain];
    self.minTableView.layer.cornerRadius = 10;
    self.minTableView.tag = 101;
    self.minTableView.backgroundColor = [UIColor brownColor];
    self.minTableView.delegate = self;
    self.minTableView.dataSource = self;
    self.minTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.minTableView.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = false;
    [self.view addSubview:_minTableView];
}
#pragma marks --- 点击标题按钮事件 ---
- (void)mainTitleButtonAction:(UIButton *)button{
    
    [UIView animateWithDuration:1.5 animations:^{
        self.minTableView.hidden = !_minTableView.hidden;
    }];
}
#pragma marks --- 创建modelarray ---
- (NSMutableArray *)creatTitleArrayWithArray:(NSArray *)array withUrlArray:(NSArray *)urlArray{
    NSMutableArray *resultArray = [NSMutableArray array];
    for (NSInteger i = 0; i < array.count; i ++) {
        MessageTitleModel *model = [[MessageTitleModel alloc]init];
        model.title = array[i];
        model.URL = urlArray[i];
        if (i == 1) {
            model.isSelect = YES;
        }else{
            model.isSelect = NO;
        }
        [resultArray addObject:model];
    }
    return resultArray;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 20;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    if (tableView.tag == 101) {
//        return _titleArray.count;
//    }else{
//        return 10;
//    }
    return _titleArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (tableView.tag == 101) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"123"];
            
        }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MessageTitleModel *model = _titleArray[indexPath.row];
    cell.textLabel.text = model.title;
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor blackColor];
    if (model.isSelect) {
        cell.backgroundColor = [UIColor grayColor];
        cell.textLabel.textColor = [UIColor redColor];
    }
    return cell;
//    }else{
//
//    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 101) {
        for (MessageTitleModel *model in _titleArray) {
            model.isSelect = NO;
        }
        MessageTitleModel *model = _titleArray[indexPath.row];
        model.isSelect = YES;
        [_mainTitleButton setTitle:model.title forState:UIControlStateNormal];
        [tableView reloadData];
    }
}

- (void)setButtonAction:(UIBarButtonItem *)button{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
